//
//  FCSDKNetworking.h
//  FCSDK
//
//  Created by ZhouYou on 2022/3/31.
//   

#import <Foundation/Foundation.h>
#import <AFNetworking/AFNetworking.h>
#import <FCSDK/FCSDKCertificateInfo.h>
#import "FCSDKEncryptionTool.h"

NS_ASSUME_NONNULL_BEGIN

@protocol AFMultipartFormData;

/**
 请求类型
 */
typedef NS_ENUM(NSInteger, FCSDKRequestType) {
    FCSDKRequestGet = 0,
    FCSDKRequestPost,
    FCSDKRequestPut,
    FCSDKRequestDelete
};

/**
 请求数据格式
 */
typedef NS_ENUM(NSInteger, FCSDKRequestSerializerType) {
    FCSDKRequestSerializerTypeJSON = 0,
    FCSDKRequestSerializerTypeHTTP
};

/**
 返回数据格式
 */
typedef NS_ENUM(NSInteger, FCSDKResponseSerializerType) {
    FCSDKResponseSerializerTypeJSON = 0,
    FCSDKResponseSerializerTypeHTTP,
    FCSDKResponseSerializerTypeXMLParser,
    FCSDKResponseSerializerTypeImage
};

/**
 请求成功回调
 */
typedef void(^FCSDKRequestSuccessBlock)(id data);

/**
 请求失败回调
 */
typedef void(^FCSDKRequestFailureBlock)(NSString *error, __nullable id data);

/**
 请求进度回调
 */
typedef void(^FCSDKNetworkingProgressBlock)(NSProgress *progress);

/**
 formdata数据
 */
typedef void(^FCSDKNetworkingBodyBlock)(id<AFMultipartFormData> data);


@interface FCSDKNetworking : NSObject

@property (nonatomic, strong) FCSDKEncryptionTool *encryptionTool;

/**请求成功回调*/
@property (nonatomic, copy, nullable) FCSDKRequestSuccessBlock successBlock;
/**请求失败回调*/
@property (nonatomic, copy, nullable) FCSDKRequestFailureBlock failureBlock;
/** 请求进度回调*/
@property (nonatomic, copy, nullable) FCSDKNetworkingProgressBlock uploadProgress;
/** formdata数据*/
@property (nonatomic, copy, nullable) FCSDKNetworkingBodyBlock bodyBlock;

/**
 requestTask
 */
@property (nonatomic, strong) NSURLSessionTask *requestTask;
/**请求的完整url*/
@property (nonatomic, strong) NSString *requestUrl;
/**请求的参数*/
@property (nonatomic, strong) id requestParam;
/**请求类型*/
@property (nonatomic, assign) FCSDKRequestType requestType;
/**请求超时时间，默认20s */
@property (nonatomic, assign) NSTimeInterval requestTimeoutInterval;
/**请求参数类型，默认为FCSDKRequestSerializerTypeJSON */
@property (nonatomic, assign) FCSDKRequestSerializerType requestSerializerType;
/**返回参数类型，默认为FCSDKResponseSerializerTypeJSON */
@property (nonatomic, assign) FCSDKResponseSerializerType responseSerializerType;
/**请求header参数*/
@property (nonatomic, strong) NSDictionary<NSString *, NSString *> * requestHeader;

/**
 证书信息，默认init时会通过FCSDKCertificateInfo加载工程配置证书信息，若FCSDKCertificateInfo中的证书信息为空则不设置；
 
 也可单独通过创建FCSDKSSLObject来设置为其它证书信息
 */
@property (nonatomic, strong) FCSDKSSLObject *sslInfo;

/**
 获取一个AFHTTPSessionManager对象，会根据系统设置自动识别是否为https
 */
+ (AFHTTPSessionManager *)fc_httpSessionManager;

/**
 获取请求的加密header参数
 */
+ (NSDictionary *)fc_EncryptHttpHeader;

/**
 获取默认的请求参数
 */
+ (NSDictionary *)fc_defaultParam;

/**
 * @brief 发起请求
 */
- (void)startRequest;

/**
 取消请求
 */
- (void)cancelRequest;

/**
 快速创建一个请求对象
 */
+ (instancetype)requestWithType:(FCSDKRequestType)requestType successBlock:(FCSDKRequestSuccessBlock)successBlock failureBlock:(FCSDKRequestFailureBlock)failureBlock;

+ (instancetype)requestWithType:(FCSDKRequestType)requestType
                  urlString:(NSString *)urlString
                      param:(nullable id)param
                         header:(NSDictionary *)header
                   successBlock:(FCSDKRequestSuccessBlock)successBlock
                   failureBlock:(FCSDKRequestFailureBlock)failureBlock;
/**
 get请求
 @param urlString 请求地址
 @param param 请求参数
 @param successBlock 成功回调
 @param failureBlock 失败回调
 @return 请求工具类
 */
+ (FCSDKNetworking *)getWithUrlString:(NSString *)urlString withParam:(nullable id)param withSuccessBlock:(FCSDKRequestSuccessBlock)successBlock withFailureBlock:(FCSDKRequestFailureBlock)failureBlock;

/**
 post请求
 @param urlString 请求地址
 @param param 请求参数
 @param successBlock 成功回调
 @param failureBlock 失败回调
 @return 请求工具类
 */
+ (FCSDKNetworking *)postWithUrlString:(NSString *)urlString withParam:(nullable id)param withSuccessBlock:(FCSDKRequestSuccessBlock)successBlock withFailureBlock:(FCSDKRequestFailureBlock)failureBlock;

/**
 @param requestType 请求类型
 @param urlString 请求地址
 @param param 请求参数
 @param successBlock 成功回调
 @param failureBlock 失败回调
 @return 请求工具类
 */
+ (FCSDKNetworking *)requestType:(FCSDKRequestType)requestType
                   withUrlString:(NSString *)urlString
                       withParam:(nullable id)param
                withSuccessBlock:(FCSDKRequestSuccessBlock)successBlock withFailureBlock:(FCSDKRequestFailureBlock)failureBlock;


/**
 @param requestType 请求类型
 @param urlString 请求地址
 @param param 请求参数
 @param encryptionTool 加密工具
 @param successBlock 成功回调
 @param failureBlock 失败回调
 @return 请求工具类
 */
+ (FCSDKNetworking *)requestType:(FCSDKRequestType)requestType
                   withUrlString:(NSString *)urlString
                       withParam:(nullable id)param
                  encryptionTool:(nullable FCSDKEncryptionTool *)encryptionTool
                withSuccessBlock:(FCSDKRequestSuccessBlock)successBlock withFailureBlock:(FCSDKRequestFailureBlock)failureBlock;


/**
 使用默认加密方式，快速创建一个请求对象
 */
+ (instancetype)requestWithDefaultEncryptType:(FCSDKRequestType)requestType
                   successBlock:(FCSDKRequestSuccessBlock)successBlock
                                 failureBlock:(FCSDKRequestFailureBlock)failureBlock;
/**
 使用默认加密工具
 @param requestType 请求类型
 @param urlString 请求地址
 @param param 请求参数
 @param successBlock 成功回调
 @param failureBlock 失败回调
 @return 请求工具类
 */
+ (FCSDKNetworking *)requestDefaultEncryptType:(FCSDKRequestType)requestType
                   withUrlString:(NSString *)urlString
                       withParam:(nullable id)param
                withSuccessBlock:(FCSDKRequestSuccessBlock)successBlock withFailureBlock:(FCSDKRequestFailureBlock)failureBlock;

/**
 使用默认加密工具的get请求
 @param urlString 请求地址
 @param param 请求参数
 @param successBlock 成功回调
 @param failureBlock 失败回调
 @return 请求工具类
 */
+ (FCSDKNetworking *)getDefaultEncryptWithUrlString:(NSString *)urlString
                            withParam:(nullable id)param withSuccessBlock:(FCSDKRequestSuccessBlock)successBlock withFailureBlock:(FCSDKRequestFailureBlock)failureBlock;

/**
 使用默认加密工具的post请求
 @param urlString 请求地址
 @param param 请求参数
 @param successBlock 成功回调
 @param failureBlock 失败回调
 @return 请求工具类
 */
+ (FCSDKNetworking *)postDefaultEncryptWithUrlString:(NSString *)urlString
                             withParam:(nullable id)param withSuccessBlock:(FCSDKRequestSuccessBlock)successBlock withFailureBlock:(FCSDKRequestFailureBlock)failureBlock;

@end

NS_ASSUME_NONNULL_END
